package com.hnkywl.sts.service.htdygl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.htdygl.HtdyDao;
import com.hnkywl.sts.dao.htdygl.HtwbDao;
import com.hnkywl.sts.dao.htdygl.HtysdyDao;
import com.hnkywl.sts.entity.htgl.Htdy;
import com.hnkywl.sts.entity.htgl.Htys;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.Constant;
import net.ruixin.orm.hibernate.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: nianjunyan
 * Date: 13-4-11
 * Time: 上午11:44
 * To change this template use File | Settings | File Templates.
 */
@Service
public class HtmbdyService {


    // 合同文本dao
    private HtwbDao htwbDao;

    // 合同定义dao
    private HtdyDao htdyDao;

    @Autowired
    public void setHtwbDao(HtwbDao htwbDao) {
        this.htwbDao = htwbDao;
    }

    @Autowired
    public void setHtdyDao(HtdyDao htdyDao) {
        this.htdyDao = htdyDao;
    }

    /**
     * 合同模板定义查询
     *
     * @param htmbmc
     * @param htdyId
     * @param pageNo
     * @param pageSize
     * @return
     */
    public String findAllHtmb(String htmbmc, String htdyId, int pageNo, int pageSize) {
        Page<Map> page = htwbDao.findAllHtmbList(htmbmc, htdyId, pageNo, pageSize);
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        String json = gson.toJson(page.getResult());
        return "{\"total\":" + page.getTotalCount() + ",\"rows\":" + json + "}";
    }


    /**
     * 根据合同模板查询合同定义
     *
     * @param htmbId
     * @return
     */
    public String getHtdyId(String htmbId) {
        String sql = "select tm.htdyid from t_biz_htmb tm where tm.id=? ";
        List list = this.htwbDao.findBySQL(sql, htmbId);
        if (null != list && list.size() > 0) {
            return list.get(0).toString();
        }
        return null;
    }

    /**
     * 删除合同模板
     *
     * @param htmbId
     */
    public void deleteHtwb(String htmbId) {
        String sql = "delete from t_biz_htmb tm where tm.id=? ";
        this.htwbDao.executeSQL(sql, htmbId);
    }

    /**
     * 根据合同模板id，查询合同模板信息
     *
     * @param htmbId
     * @return
     */
    public List findHtmbById(String htmbId) {
        String sql = "select tm.id,tm.htmbmc,tm.htdyid,tm.bz from t_biz_htmb tm where tm.id=? ";
        List list = this.htwbDao.findBySQL(sql, htmbId);
        if (null != list && list.size() > 0) {
            return list;
        }
        return null;
    }

    /**
     * 保存或者更新合同模板基本信息
     *
     * @param htmbId
     * @param htmb_mc
     * @param htmb_htdy
     * @param htmb_bz
     * @return
     */
    public String addOrUpdateHtmb(String htmbId, String htmb_mc, String htmb_htdy, String htmb_bz) {

        String seqSql = "select  Seq_Biz_Htmb.Nextval from dual ";
        String id = this.htwbDao.createSQLQuery(seqSql).uniqueResult().toString();
        //如果 名称为空
        if (StringUtils.isEmpty(htmb_mc)) {
            Htdy htdy = htdyDao.get(Long.parseLong(htmb_htdy));    //获取合同定义
            htmb_mc = htdy.getMc() + "_" + id;   //默认名
        }
        if (StringUtils.isEmpty(htmbId)) {
            String saveSql = "insert into t_biz_htmb(id,jgid,htmbmc,htdyid,date_created,bz) values" +
                    "(" + Long.valueOf(id) + "," + YgService.getCurrentYgGsId() + ",'" + htmb_mc + "'," + htmb_htdy + ",sysdate,'" + htmb_bz + "')";
            this.htwbDao.executeSQL(saveSql);
            return id;
        } else {
            String updateSql = "update t_biz_htmb  tm set tm.htmbmc=?,tm.htdyid=?,tm.last_updated=sysdate,tm.bz=? where tm.id=?";
            this.htwbDao.executeSQL(updateSql, htmb_mc, htmb_htdy, htmb_bz, htmbId);
            return htmbId;
        }
    }

    /**
     * 查询合同模板，根据合同定义id
     *
     * @param htdyId
     * @return
     */
    public String findAllHtmbByHtdyID(String htdyId) {
        StringBuffer htmbJson = new StringBuffer();
        // 转成json格式
        String htmbSql = "select id,htmbmc from t_biz_htmb where htdyid=? and jgid=?";
        List htmbList = this.htwbDao.findBySQL(htmbSql, htdyId, YgService.getCurrentYgGsId());
        if (null != htmbList && htmbList.size() > 0) {
            for (int i = 0; i < htmbList.size(); i++) {
                Object[] objects = (Object[]) htmbList.get(i);
                htmbJson.append("{").append("\"id\"").append(":\"").append(objects[0]).append("\"").append(",").
                        append("\"htmbmc\"").append(":\"").append(objects[1]).append("\"").append("}").append(",");
            }
        }
        if (htmbJson.length() > 0) {
            return "[" + htmbJson.substring(0, htmbJson.length() - 1) + "]";
        }
        return null;

    }
}
